package com.jojo.android.zxlib.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.jojo.android.zxlib.R;
import com.jojo.android.zxlib.tool.ZxLogTool;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 创建者 zx
 * 创建时间 2020/12/10 15:17
 */
public abstract class BaseDialog extends Dialog {
    private final String TAG = "BaseDialog";
    private Unbinder unbinder;
    protected View rootView;
    protected OnClickListener onClickListener;
    protected Activity activity;

    public static final int BUTTON_CANCEL = 1;
    public static final int BUTTON_SUBMIT = 2;

    public BaseDialog(@NonNull Fragment context) {
        this(context.getActivity());
    }

    private BaseDialog(Context context) {
        super(context, R.style.mDialog);
    }

    public BaseDialog(Activity context) {
        super(context, R.style.mDialog);
        this.activity = context;
        initialize();
    }

    private void initialize() {
        try {
            rootView = LayoutInflater.from(getContext()).inflate(getLayoutId(), null, false);
            setContentView(rootView);
            unbinder = ButterKnife.bind(this);
            setCancelable(true);
            setCanceledOnTouchOutside(false);
            init();
        } catch (Exception e) {
            ZxLogTool.err(e);
        }

    }

    protected abstract void init();

    protected abstract int getLayoutId();

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onClick(Dialog dialog, int button);
    }

}
